package top.juntower.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.juntower.pojo.EnjoyFoot;
import top.juntower.pojo.FoodMatching;
import top.juntower.pojo.User;
import top.juntower.pojo.dto.FoodMatchingDto;
import top.juntower.pojo.dto.FootDta;
import top.juntower.pojo.vo.EnjoyFootVo;
import top.juntower.pojo.vo.FoodMatchingVo;
import top.juntower.service.FoodMatchingService;
import top.juntower.utils.JsonResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "食物搭配")
@RestController
@RequestMapping("foodmatching")
public class FoodMatchingController {
    @Autowired
    FoodMatchingService foodMatchingService;

    //搭配信息
    @ApiOperation(value = "搭配信息",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/queryFoodMatching")
    public JsonResult queryFoodMatching(Integer pageNum){
        if (pageNum == null){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,4);
        List<FoodMatchingVo> foodMatchingVos = foodMatchingService.queryFoodMatching(pageNum,4);
        PageInfo<FoodMatchingVo> pageInfo = new PageInfo<FoodMatchingVo>(foodMatchingVos);
        if (foodMatchingVos  == null){
            return JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(pageInfo);
        }
    }

    //查询搭配详细信息
    @ApiOperation(value = "查询搭配详细信息",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fmId", value = "食物搭配Id", dataType = "Integer", required = true)
    })
    @GetMapping("/queryFoodMatchinginformation")
    public JsonResult queryFoodMatchinginformation(Integer fmId){
        if (fmId==null){
            return  JsonResult.error(JsonResult.NO);
        }
        List<FoodMatching> foodMatchings = foodMatchingService.queryFoodMatchinginformation(fmId);
        if (foodMatchings == null){
            return  JsonResult.error(JsonResult.MISS);
        }else {
            return JsonResult.ok(foodMatchings);
        }
    }

    //增加访问数量
    @ApiOperation(value = "增加访问数量",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fmId", value = "食物搭配Id", dataType = "Integer", required = true)
    })
    @GetMapping("/addFoodMatchingVisits")
    public JsonResult addFoodMatchingVisits(Integer fmId){
        if (fmId==null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = foodMatchingService.addFoodMatchingVisits(fmId);
        if (i>0){
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }
    }

    //食物搭配上一页
    @ApiOperation(value = "食物搭配上一页",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fmId", value = "食物搭配Id", dataType = "Integer", required = true)
    })
    @GetMapping("/querylastPage")
    public JsonResult querylastPage(Integer fmId){
        if (fmId == null){
            return  JsonResult.error(JsonResult.NO);
        }
        FoodMatchingDto foodMatchingDto = foodMatchingService.querylastPage(fmId);
        if (foodMatchingDto == null){
            return JsonResult.error("无");
        }else {
            return  JsonResult.ok(foodMatchingDto);
        }
    }

    //食物搭配下一页
    @ApiOperation(value = "食物搭配下一页",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fmId", value = "食物搭配Id", dataType = "Integer", required = true)
    })
    @GetMapping("/queryNextPage")
    public JsonResult queryNextPage(Integer fmId){
        if (fmId == null){
            return  JsonResult.error(JsonResult.NO);
        }
        FoodMatchingDto foodMatching = foodMatchingService.queryNextPage(fmId);
        if (foodMatching == null){
            return JsonResult.error("无");
        }else {
            return  JsonResult.ok(foodMatching);
        }
    }
    //食物搭配信息后台管理
    @ApiOperation(value = "食物搭配信息后台管理",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true)
    })
    @GetMapping("/queryFoodMatchingBackstage")
    public JsonResult queryFoodMatchingBackstage(Integer pageNum){
        if (pageNum == null){
            return  JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,8);
        List<FoodMatching> foodMatchings = foodMatchingService.queryFoodMatchingBackstage(pageNum, 8);
        PageInfo<FoodMatching> pageInfo = new PageInfo<FoodMatching>(foodMatchings);
        if (foodMatchings  == null){
            return JsonResult.error(JsonResult.MISS);
        }else {
            return  JsonResult.ok(pageInfo);
        }
    }

    //删除食物搭配信息
    @ApiOperation(value = "删除食物搭配信息",response = FoodMatching.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fmId", value = "食物搭配Id", dataType = "Integer", required = true)
    })
    @GetMapping("/deleteFoodMatchingById")
    public JsonResult deleteFoodMatchingById(Integer fmId){
        if (fmId == null){
            return  JsonResult.error(JsonResult.NO);
        }
        int i = foodMatchingService.deleteFoodMatchingById(fmId);
        if (i>0){
            return  JsonResult.ok(JsonResult.DEL_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.DEL_MISS);
        }
    }


    //食物搭配批量删除
    @ApiOperation(value = "食物搭配批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "moreFmId", value = "多个食物搭配Id", dataType = "String", required = true),
    })
    @GetMapping("/deleteMoreFoodMatching")
    public JsonResult deleteMoreFoodMatching(String moreFmId) {
        if (moreFmId == null) {
            JsonResult.error(JsonResult.NO);
        }
        List<Integer> del_ids=new ArrayList<>();
        String[] str_ids=moreFmId.split(",");
        for (String str_id : str_ids) {
            del_ids.add(Integer.parseInt(str_id));
        }
        int i = foodMatchingService.deleteMoreFoodMatching(del_ids);
        if (i>0){
            return JsonResult.ok(JsonResult.DEL_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.DEL_MISS);
        }
    }

    //新增食物搭配后台管理
    @ApiOperation(value = "新增食物搭配后台管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fmTile", value = "食物搭配标题", dataType = "String", required = true),
            @ApiImplicitParam(name = "fmImg", value = "食物搭配图片", dataType = "String", required = true),
            @ApiImplicitParam(name = "fmContent", value = "食物搭配内容", dataType = "String", required = true)
    })
    @GetMapping("/addFoodMatching")
    public JsonResult addFoodMatching(String fmTile,String fmImg,String fmContent) {
        if (fmTile == null || fmImg == null || fmContent == null) {
            JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("fmTile",fmTile);
        map.put("fmImg",fmImg);
        map.put("fmContent",fmContent);
        int i = foodMatchingService.addFoodMatching(map);
        if (i>0){
            return  JsonResult.ok(JsonResult.INSERT_SUCCESS);
        }else {
            return  JsonResult.error(JsonResult.INSERT_MISS);
        }
    }
}
